<?php


namespace app\admin\controller\User;


use app\admin\controller\BaseController;
use app\admin\controller\Merchant\Merchant;
use app\admin\model\Merchant\MerchantModel;
use app\admin\model\User\UserFansModel;
use app\admin\model\User\UserModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use Exception;
use think\facade\Db;
use think\Request;

class User extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getUserList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $account = !empty($param['key_word']) ? $param['key_word'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if (empty($page)) {
            $page = 1;
        }
        if (empty($limit)) {
            $limit = 10;
        }
        if (!empty($account)) {
            array_push($where, ['account', 'like', '%' . $account . '%']);
        }
        if (!empty($is_show)) {
            if ($is_show == 1) {
                array_push($where, ['is_show', '=', 0]);
            }
            if ($is_show == 2) {
                array_push($where, ['is_show', '=', 1]);
            }
        }
        $list = UserModel::getUserListWhere($page, $limit, $where);
        $count = UserModel::getUserCountWhere($where);
        foreach ($list as &$item) {
            $item['head_img'] = ImageUtil::i()->replaceUrl($item['head_img']);
        }
        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");

    }

    /**
     * 编辑用户信息
     */
    public function edit_user(Request $request)
    {
        $id = (int)$request->param('id');
        $nick_name = (string)$request->param('nickname');
        $account = (int)$request->param('account');
        if (empty($id)) {
            return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        }

        $save = [
            'mer_name' => $nick_name,
            'up_time' => date("Y-m-d H:i:s", time())
        ];
        $flag = Db::name('merchant')->where('account', $account)->update($save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败" . $account);
        }
    }

    public function edit_user_add(Request $request)
    {
        $id = (int)$request->param('id');
        $nick_name = (string)$request->param('nickname');
        if (empty($id)) {
            return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        }

        $save = [
            'nickname' => $nick_name,
            'identity' => 1,
            'up_time' => date("Y-m-d H:i:s", time())
        ];
        Db::transaction(function () use ($id, $save) {
            UserModel::updateUserWhere([['id', '=', $id]], $save);
            $user = Db::name('user')->where('id', $id)->find();
            $data = [
                'account' => $user['account'],
                'mer_phone' => $user['mobile'],
                'mer_name' => $user['nickname'],
                'is_open' => 1,
                'audit_status' => 1,
                'user_id' => $id,
                'add_time' => date("Y-m-d H:i:s", time()),
                'up_time' => date("Y-m-d H:i:s", time()),
            ];
            Db::name('merchant')->insert($data);
        });
        return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
    }


    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delUser(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try {
            //中间逻辑代码
            $flag = UserModel::delUserWhere([['id', '=', $id]]);
            if ($flag) {
                //删除之前的属性
                UserFansModel::delUserFansWhere([['uid', '=', $id]]);
                DB::commit();
                return Util::returnJson(self::CODE_SUCCESS, null, "删除成功");
            } else {
                DB::rollBack();
                return Util::returnJson(self::CODE_ERROR, null, "删除失败");
            }
        } catch (Exception $e) {
            //接收异常处理并回滚
            //回滚事务
            DB::rollBack();
            return Util::returnJson(self::CODE_ERROR, $e, "删除失败");
        }
    }

    /**
     * 查询所有用户
     */
    public function all(Request $request)
    {
        $account = (string)$request->param('account', '');
        $info = UserModel::where($account, function ($query) use ($account) {
            return $query->where('account', 'like', '%' . $account . '%');
        })->field('id,account as name ,nickname')->select()->toArray();
        return Util::returnJson(self::CODE_SUCCESS, $info, "ok");
    }

}
